package com.gxa.controller;

import com.gxa.pojo.Admin;
import com.gxa.service.AdminService;
import com.gxa.utils.ResponseData;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2022-09-02 22:31:31
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;
    
    /**
     * 通过主键查询单条数据
     *
     * @param username 用户名
     * @param password 密码
     * @return 单条数据
     */
    @ApiOperation(value="登录",httpMethod = "POST",response = ResponseData.class)
    @PostMapping("/login")
    public ResponseData login(@ApiParam(name = "username" ,value = "用户名", required = true)String username,
                              @ApiParam(name = "password" ,value = "密码",required = true)String password) {
        ResponseData responseData = new ResponseData();
        //使用 shiro 登录验证
        //1 认证的核心组件：获取 Subject 对象
        Admin admin = new Admin();
        admin.setAdminName(username);
        admin.setAdminPassword(password);
        Subject subject = SecurityUtils.getSubject();
        //2 将登陆表单封装成 token 对象
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getAdminName(), admin.getAdminPassword());
        try {
            //3 让 shiro 框架进行登录验证：
            subject.login(token);
            responseData.setMsg("success");
        } catch (Exception e) {
            e.printStackTrace();
            responseData.setMsg("登陆失败，请重新输入用户名密码");
        }
       return responseData;
    }

    /**
     * 新增数据
     *
     * @param admin 实体
     * @return 新增结果
     */
//    @PostMapping
//    public ResponseEntity<Admin> add(Admin admin) {
//        return ResponseEntity.ok(this.adminService.insert(admin));
//    }

    /**
     * 编辑数据
     *
     * @param admin 实体
     * @return 编辑结果
     */
//    @PutMapping
//    public ResponseEntity<Admin> edit(Admin admin) {
//        return ResponseEntity.ok(this.adminService.update(admin));
//    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
//    @DeleteMapping
//    public ResponseEntity<Boolean> deleteById(Integer id) {
//        return ResponseEntity.ok(this.adminService.deleteById(id));
//    }

}

